package com.ep.thirdPlatformProject.mq.controller;

import cn.hutool.core.util.IdUtil;
import com.ep.thirdPlatformProject.dhg.entity.DhgDepartmentEntity;
import com.ep.thirdPlatformProject.dhg.entity.DhgEmployeeEntity;
import com.ep.thirdPlatformProject.dhg.entity.vo.DhgDepartmentVO;
import com.ep.thirdPlatformProject.dhg.service.DhgDepartmentService;
import com.ep.thirdPlatformProject.dhg.service.impl.DhgDepartmentServiceImpl;
import com.ep.thirdPlatformProject.job.common.AjaxResult;
import com.ep.thirdPlatformProject.oa.service.HrmResourceService;
import com.ep.thirdPlatformProject.oa.service.OAApiService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
//import org.apache.rocketmq.client.producer.TransactionSendResult;
//import org.apache.rocketmq.spring.core.RocketMQTemplate;
//import org.apache.rocketmq.spring.support.RocketMQHeaders;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import weaver.hrm.webservice.UserBean;

import java.util.Date;
import java.util.List;
import java.util.Objects;


@Slf4j
@RequiredArgsConstructor
@Tag(name = "测试消息发送")
@Controller
public class ProducerController {

    private final HrmResourceService hrmResourceService;

//    private final RocketMQTemplate rocketMQTemplate;

    private final ChatClient chatClient;

    private final DhgDepartmentService dgDepartmentService;

    private final OAApiService aoApiService;
    private final DhgDepartmentServiceImpl dhgDepartmentServiceImpl;

//    @PostMapping("/sendOneWayMsg")
//    public String sendMsg(HttpServletRequest request, HttpServletResponse response, String data) {
//        try {
//
//            long uuid = IdUtil.getSnowflakeNextId();
//            String msgStr = "我测试的第二条消息！";
//            // 构建消息对象
//            Message<String> build = MessageBuilder.withPayload(msgStr)
//                    .setHeaderIfAbsent("KEYS", "second")
//                    .setHeader(RocketMQHeaders.TRANSACTION_ID, uuid)
//                    .build();
//            String dest = String.format("%s:%s", "karl_mq_test", "tag1");
//            String msgId = rocketMQTemplate.syncSendDelayTimeSeconds(dest, build, 1000).getMsgId();
//            log.info("msgId:{}", msgId);
//            return msgId;
//        } catch (Exception e) {
//            log.error("e:{}", e.getMessage(), e);
//            throw new RuntimeException(e);
//        }
//
//    }


//    @GetMapping("/sendTxMsg")
//    public String sendTxMsg() {
//        try {
//            long uuid = IdUtil.getSnowflakeNextId();
//            String msgStr = "我测试的事务消息第1条消息！";
//            // 构建消息对象
//            org.springframework.messaging.Message<String> build = MessageBuilder.withPayload(msgStr)
//                    .setHeader(RocketMQHeaders.TRANSACTION_ID, uuid).build();
//            TransactionSendResult result = rocketMQTemplate.sendMessageInTransaction("TransactionProducer", build, null);
//            log.info("status:{}", result.getLocalTransactionState());
//            return result.getMsgId();
//        } catch (Exception e) {
//            log.error("e:{}", e.getMessage(), e);
//            throw new RuntimeException(e);
//        }
//
//    }


    @GetMapping(value = "/ai/simple", produces = "application/json")
    @ResponseBody
    public AjaxResult testAi(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        try {
            if (Objects.equals(message, "sb") || message.contains("sb")) {
                throw new RuntimeException("你文明点！！！");
            }
            String content = chatClient.prompt().user(message).call().content();
            List<DhgDepartmentEntity> list = dgDepartmentService.list();
            return AjaxResult.success("操作成功",list);
        } catch (Exception e) {
            log.error("e:{}", e.getMessage(), e);
            throw new RuntimeException(e);
        }

    }


    @GetMapping(value = "/ai/test/function", produces = "application/json")
    public String testFunction(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        try {
            DhgEmployeeEntity entity = new DhgEmployeeEntity();
            entity.setEmpNo("21240");
            entity.setEmpName("游艳龙");
            entity.setCompanyCode("ZJEP");
            entity.setCompanyName("浙江中力机械股份有限公司");
            entity.setOrgCode("ZJEP040");
            entity.setOrgName("国内售后>西北区(售后)");
            entity.setEntryDate(new Date());
            entity.setSex("男");
            entity.setMobile("17303489423");
            entity.setWorkEmail("");
            entity.setWorkPlace("山西");
            entity.setPostCode("P1540");
            entity.setPostName("售后维修员");
            entity.setLeaderEmpNo("06213");
            entity.setNeedAttendance("1");
            DhgDepartmentVO department = dhgDepartmentServiceImpl.getDepartmentTreeByCode(entity.getOrgCode());
//            UserBean userBean = hrmResourceService.firstSaveOAUser(entity);

            UserBean userBean = new UserBean();
//            WorkflowRequestInfo requestInfo =aoApiService. buildOnboardingRequestInfo(userBean, DateUtil.formatDate(entity.getEntryDate()), entity.getNeedAttendance());
            return userBean.toString();
        } catch (Exception e) {
            log.error("e:{}", e.getMessage(), e);
            throw new RuntimeException(e);
        }

    }


    @GetMapping(value = "/ai/test/gen", produces = "application/json")
    public String testGenericFunction(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        try {
//            DhgDepartmentEntity departmentTree = dgDepartmentService.getDepartmentTreeByCode();
            return "1";
        } catch (Exception e) {
            log.error("e:{}", e.getMessage(), e);
            throw new RuntimeException(e);
        }

    }

    @GetMapping("/index")
    public String toIndex(String userId) {
        return "index";
    }

}
